package com.example.administrator.yanfoxconn.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;

import com.example.administrator.yanfoxconn.R;
import com.example.administrator.yanfoxconn.adapter.EmpListAdapter;
import com.example.administrator.yanfoxconn.adapter.KKCheckListAdapter;
import com.example.administrator.yanfoxconn.bean.KKGWMessage;
import com.example.administrator.yanfoxconn.constant.Constants;
import com.example.administrator.yanfoxconn.constant.FoxContext;
import com.example.administrator.yanfoxconn.utils.BaseActivity;
import com.example.administrator.yanfoxconn.utils.HttpUtils;
import com.example.administrator.yanfoxconn.utils.ToastUtils;
import com.example.administrator.yanfoxconn.widget.MyListView;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @Description 關務-設備盤點進度表
 * @Author song
 * @Date 10/19/21 7:04 PM
 */
public class KKCHeckListActivity extends BaseActivity implements View.OnClickListener {
    private final int MESSAGE_SET_GWYC = 111;//GWYC
    @BindView(R.id.btn_title_left)
    Button btnBack;//返回
    @BindView(R.id.tv_title)
    TextView tvTitle;//標題
    @BindView(R.id.lv_list)
    MyListView lvList;

    private List<KKGWMessage> message;
    private KKCheckListAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kk_check_list);
        ButterKnife.bind(this);

        tvTitle.setText("點檢記錄");
        btnBack.setOnClickListener(this);
        getList(FoxContext.getInstance().getLoginId());

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_title_left:
                finish();
                break;
        }
    }
    private void getList(String keeper) {
        String nameE = "";
        try {//姓名改為工號
            nameE = URLEncoder.encode(URLEncoder.encode(keeper, "UTF-8"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        showDialog();

        final String url = Constants.HTTP_GW_PROGRESS + "?keeper=" + nameE ;
        Log.e("-----------", "url==" + url);
        new Thread() {
            @Override
            public void run() {
                //把网络访问的代码放在这里
                String result = HttpUtils.queryStringForGet(url);
                dismissDialog();
                Gson gson = new Gson();
                if (result != null) {
                    Log.e("---------", "result==fff===" + result);
                    JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                    String errCode = jsonObject.get("errCode").getAsString();
                    if (errCode.equals("200")) {
                        JsonArray array = jsonObject.get("data").getAsJsonArray();

                        message = new ArrayList<KKGWMessage>();
                        for (JsonElement type : array) {
                            KKGWMessage humi = gson.fromJson(type, KKGWMessage.class);
                            message.add(humi);
                        }

                        Message message = new Message();
                        message.what = Constants.MESSAGE_SET_TEXT;
                        message.obj = jsonObject.get("errMessage").getAsString();
                        mHandler.sendMessage(message);

                    } else {
                        Log.e("-----------", "result==" + result);
                        Message message = new Message();
                        message.what = Constants.MESSAGE_TOAST;
                        message.obj = jsonObject.get("errMessage").getAsString();
                        mHandler.sendMessage(message);
                    }
                } else {
                    Message message = new Message();
                    message.what = Constants.MESSAGE_NETMISTAKE;
                    mHandler.sendMessage(message);
                    finish();
                }
            }
        }.start();
    }
    Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Constants.MESSAGE_TOAST://Toast彈出
                        ToastUtils.showLong(KKCHeckListActivity.this, msg.obj.toString());
                    break;
                case Constants.MESSAGE_NETMISTAKE://Toast彈出
                    ToastUtils.showLong(KKCHeckListActivity.this,R.string.net_mistake);
                    break;
                case Constants.MESSAGE_SET_TEXT://text賦值

                    adapter = new KKCheckListAdapter(KKCHeckListActivity.this,message);
                    lvList.setAdapter(adapter);

                    adapter.setOnClickListener(new KKCheckListAdapter.OnClickListener() {
                        @Override
                        public void OnClickListenerYJ(int position, String flag) {
                            Intent intent = new Intent(KKCHeckListActivity.this,KKCHeckList2Activity.class);
                            intent.putExtra("feeCode",message.get(position).getFEECODE());
                            intent.putExtra("flag",flag);
                            startActivity(intent);
                            Log.e("--------","YJ");
                        }

                        @Override
                        public void OnClickListenerWJ(int position, String flag) {

                            Intent intent = new Intent(KKCHeckListActivity.this,KKCHeckList2Activity.class);
                            intent.putExtra("feeCode",message.get(position).getFEECODE());
                            intent.putExtra("flag",flag);
                            startActivity(intent);
                            Log.e("--------","WJ");
                        }

                        @Override
                        public void OnClickListenerYC(int position, String flag) {

                            Intent intent = new Intent(KKCHeckListActivity.this,KKCHeckList2Activity.class);
                            intent.putExtra("feeCode",message.get(position).getFEECODE());
                            intent.putExtra("flag",flag);
                            startActivity(intent);
                            Log.e("--------","YC");
                        }
                    });
                    break;
            }
            super.handleMessage(msg);
        }
    };
}
